package com.finace.money.presenter;

import com.finace.money.common.util.ACache;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.QryData;
import com.finace.money.data.bean.QryOrderBean;
import com.finace.money.data.bean.QryPositionBean;
import com.finace.money.data.bean.QryTradeBean;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.data.bean.TickBean;
import com.finace.money.net.socket.PushReceiver;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangpeng on 2018/1/11.
 */

public class MainHavePresenter extends BasePresenter {

    private ArrayList<QryData> qryData = new ArrayList<>();

    public MainHavePresenter() {
        super();
    }

    public List<QryData> getPositionData(TickBean tickBean) {

        ArrayList<QryPositionBean> mQryPositionLists = (ArrayList<QryPositionBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.QRY_POSITION_END);
        ArrayList<QryOrderBean> mQryOrderLists = (ArrayList<QryOrderBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.QRY_ORDER_END);
        qryData.clear();
        if (null != mQryPositionLists && mQryPositionLists.size() > 0 ) {
            for (QryPositionBean qryPositionBean : mQryPositionLists) {
                int useCount = qryPositionBean.getPosition().getPosition();
                double fudong = 0;
                String whichDay;
                if(null != mQryOrderLists && mQryOrderLists.size() > 0){
                    for (QryOrderBean qryOrderBean : mQryOrderLists) {
                        if (qryPositionBean.getPosition().getSymbol().equals(qryOrderBean.getOrder().getSymbol()) &&
                                qryPositionBean.getPosition().isSide() != qryOrderBean.getOrder().isSide() && (
                                qryOrderBean.getOrder().getStatus().equals("Placed") ||
                                        qryOrderBean.getOrder().getStatus().equals("Submited") ||
                                        qryOrderBean.getOrder().getStatus().equals("Opened") ||
                                        qryOrderBean.getOrder().getStatus().equals("PartFilled"))) {
                            useCount = qryPositionBean.getPosition().getPosition() -
                                    (qryOrderBean.getOrder().getTotalSize() - qryOrderBean.getOrder().getFilledSize());
                            break;
                        }
                    }
                }else {
                    useCount = qryPositionBean.getPosition().getPosition();
                }

                /*if (qryPositionBean.getPosition().isSide()) {//多
                    fudong = (Double.valueOf(tickBean.getTrade()) - qryPositionBean.getPosition().getAvg_price()) *
                            qryPositionBean.getPosition().getPosition() * qryPositionBean.getPosition().getMultiple();
                } else {//空
                    fudong = (qryPositionBean.getPosition().getAvg_price() - Double.valueOf(tickBean.getTrade())) *
                            qryPositionBean.getPosition().getPosition() * qryPositionBean.getPosition().getMultiple();
                }*/
//                qryPositionBean.getPosition().getToday_position()
                if (qryPositionBean.getPosition().getToday_position() == 0) {
                    if (qryPositionBean.getPosition().getYd_position() == 0) {
                        whichDay = null;
                    } else {
                        whichDay = "昨仓";
                    }
                } else {
                    if (qryPositionBean.getPosition().getYd_position() == 0) {
                        whichDay = "今仓";
                    } else {
                        whichDay = "今-昨";
                    }
                }
                if(qryPositionBean.getPosition().getPosition() != 0){
                    qryData.add(new QryData(whichDay, qryPositionBean.getPosition().getSymbol(),
                            qryPositionBean.getPosition().isSide() ? "多" : "空",
                            String.valueOf(qryPositionBean.getPosition().getAvg_price()),
                            String.valueOf(qryPositionBean.getPosition().getPosition()),
                            String.valueOf(useCount),
                            String.valueOf(fudong),
                            null, null, String.valueOf(qryPositionBean.getPosition().getToday_position())));
                }
            }
        }
        return qryData;
    }

    public List<QryData> getOrderKillData() {
        ArrayList<QryOrderBean> mQryOrderLists = (ArrayList<QryOrderBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.QRY_ORDER_END);

        qryData.clear();
        if (null != mQryOrderLists && mQryOrderLists.size() > 0) {
            for (QryOrderBean qryOrderBean : mQryOrderLists) {
                if (qryOrderBean.getOrder().getStatus().equals("Placed") ||
                        qryOrderBean.getOrder().getStatus().equals("Submited") ||
                        qryOrderBean.getOrder().getStatus().equals("Opened") ||
                        qryOrderBean.getOrder().getStatus().equals("PartFilled")) {
                    QryData data = new QryData(null, qryOrderBean.getOrder().getSymbol(),
                            qryOrderBean.getOrder().isSide() ? "买" : "卖",
                            qryOrderBean.getOrder().getOffsetFlag(),
                            String.valueOf(qryOrderBean.getOrder().getLimitPrice()),
                            String.valueOf(qryOrderBean.getOrder().getTotalSize() - qryOrderBean.getOrder().getFilledSize()),
                            null, null, null, null);
                    data.setOrderId(String.valueOf(qryOrderBean.getOrder().getId()));
                    this.qryData.add(data);
                }
            }
        }
        return qryData;
    }

    public List<QryData> getOrderPleaseData() {
        ArrayList<QryOrderBean> mQryOrderLists = (ArrayList<QryOrderBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.QRY_ORDER_END);
        qryData.clear();

        if (null != mQryOrderLists && mQryOrderLists.size() > 0) {
            for (QryOrderBean qryOrderBean : mQryOrderLists) {
                String status = null;
                switch (qryOrderBean.getOrder().getStatus()) {
//                    case "Placed": //可撤
//                        status = "提交";
//                        break;
//                    case "Submited": //可撤
//                        status = "可撤";
//                        break;
//                    case "Broker": //可撤
//                        status = "可撤";
//                        break;
//                    case "Opened"://可撤
//                        status = "待成";
//                        break;
                    case "Filled": //委托，
                        status = "成交";
                        break;
//                    case "PartFilled"://可撤
//                        status = "部成";
//                        break;
                    case "Canceled"://委托
                        status = "取消";
                        break;
                    case "Reject"://委托
                        status = "拒绝";
                        break;
                    /*default:
                        status = "例外";
                        break;*/
                }
                if (!(qryOrderBean.getOrder().getStatus().equals("Placed") ||
                        qryOrderBean.getOrder().getStatus().equals("Submited") ||
                        qryOrderBean.getOrder().getStatus().equals("Opened") ||
                        qryOrderBean.getOrder().getStatus().equals("PartFilled"))) {
                    qryData.add(new QryData(null, qryOrderBean.getOrder().getSymbol(),
                            status,
                            qryOrderBean.getOrder().isSide() ? "多" : "空",
                            qryOrderBean.getOrder().getOffsetFlag(),
                            String.valueOf(qryOrderBean.getOrder().getLimitPrice()),
                            String.valueOf(qryOrderBean.getOrder().getTotalSize()),
                            String.valueOf(qryOrderBean.getOrder().getFilledSize()),
                            String.valueOf(qryOrderBean.getOrder().getDate()) + String.valueOf(qryOrderBean.getOrder().getTime()),
                            null));
                }
            }
        }

        return qryData;
    }

    public List<QryData> getTradeData() {
        ArrayList<QryTradeBean> mQryTradeLists = (ArrayList<QryTradeBean>) ACache.get(UIUtil.getContext()).getAsObject(PushReceiver.QRY_TRADE_END);
        qryData.clear();
        if (null != mQryTradeLists && mQryTradeLists.size() > 0) {
            for (QryTradeBean qryTradeBean : mQryTradeLists) {
                qryData.add(new QryData(null, qryTradeBean.getTrade().getSymbol(),
                        qryTradeBean.getTrade().isSide() ? "多" : "空",
                        qryTradeBean.getTrade().getOffset_flag_str(),
                        String.valueOf(qryTradeBean.getTrade().getXprice()),
                        String.valueOf(qryTradeBean.getTrade().getXsize()),
                        String.valueOf(qryTradeBean.getTrade().getXdate()) + String.valueOf(qryTradeBean.getTrade().getXtime()),
                        null, null, null));
            }
        }

        return qryData;
    }

    public int getDealPosition(String id, List<StatusListBean> mStatusLists) {

        if (StringUtil.isNotNullString(id)) {
            for (int i = 0; i < mStatusLists.size(); i++) {
                if (mStatusLists.get(i).getM_Body().getM_ProductID().equals(id)) {
                    return i;
                }
            }
        }
        return 0;
    }

    public ArrayList<QryData> getHaveData(String m_productID, ArrayList<QryData> mHaves, TickBean mTickBean) {
        mHaves.clear();
        List<QryData> positionData = getPositionData(mTickBean);
        for (int i = 0; i < positionData.size(); i++) {
            if (positionData.get(i).getText1().equals(m_productID)) {
                mHaves.add(positionData.get(i));
            }
        }
        return mHaves;
    }

    public int getTodaySize(String side, ArrayList<QryData> mHaves) {

        int count = 0;
        for (QryData qry:mHaves) {
            if(side.equals("true") && qry.getText2().equals("多")&&StringUtil.isNotNullString(qry.getText9())){
                count = Integer.valueOf(qry.getText9());
            }
        }
        return count;
    }
}
